/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.jfonia.view.panels;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.Rectangle;
import javax.swing.JPanel;
import org.jfonia.connect5.basics.MutableValueNode;

/**
 * JPanel with data fields xNode, yNode, widthNode and heightNode,
 * which are set when the bounds of this component are changed.
 * The panel does not observe these variables, that's the
 * responsibility of the inherited class
 * 
 * @author Rik Bauwens
 */
public abstract class NodePanel extends JPanel {

        protected MutableValueNode<Double> xNode;
        protected MutableValueNode<Double> yNode;
        protected MutableValueNode<Double> widthNode;
        protected MutableValueNode<Double> heightNode;
    
        public NodePanel(MutableValueNode<Double> xNode, MutableValueNode<Double> yNode,
                              MutableValueNode<Double> widthNode, MutableValueNode<Double> heightNode)
        {
                super();
                this.xNode = xNode;
                this.yNode = yNode;
                this.widthNode = widthNode;
                this.heightNode = heightNode;
                setLayout(null);
        }

        public MutableValueNode<Double> getxNode()
        {
                return xNode;
        }

        public MutableValueNode<Double> getyNode()
        {
                return yNode;
        }

        public MutableValueNode<Double> getWidthNode()
        {
                return widthNode;
        }

        public MutableValueNode<Double> getHeightNode()
        {
                return heightNode;
        }

        //Override setLocation(x, y)
        //When x/y are changed, the nodes need to be updated as well
        @Override
        public void setLocation(int x, int y)
        {
                super.setLocation(x, y);
                //Avoid propagation
                if(x != getX())
                        xNode.setValue(new Double(x));
                if(y != getY())
                        yNode.setValue(new Double(y));
        }

        //Call setLocation(x, y)
        @Override
        public void setLocation(Point p)
        {
                setLocation(p.x, p.y);
        }

        //Override the setBounds(x, y, width, height) method
        //When new boundaries are set, the nodes are aught to be updated
        @Override
        public void setBounds(int x, int y, int width, int height)
        {
                super.setBounds(x, y, width, height);
                //Avoid propagation
                if(x != getX())
                        xNode.setValue(new Double(x));
                if(y != getY())
                        yNode.setValue(new Double(y));
                if(width != getWidth())
                        widthNode.setValue(new Double(width));
                if(height != getHeight())
                        heightNode.setValue(new Double(height));
        }

        //Call setBounds (x, y, width, height)
        @Override
        public void setBounds(Rectangle r)
        {
                setBounds(r.x, r.y, r.width, r.height);
        }

        //Override setPreferredSize(preferredSize) method
        //Thus, set the widthNode/heightNode
        @Override
        public void setPreferredSize(Dimension preferredSize)
        {
                super.setPreferredSize(preferredSize);
                widthNode.setValue(preferredSize.getWidth());
                heightNode.setValue(preferredSize.getHeight());
        }
}
